package com.anlogic.sdk.bsp.view.ui;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.IFormPage;

public class HPFEditor extends FormEditor {
	private HPFViewer hpfViewer;
	
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		if (!(input instanceof IFileEditorInput))
			throw new PartInitException("Invalid Input: Must be IFileEditorInput");
		super.init(site, input);
	}
	
	@Override
	protected void addPages() {
		try {
			this.hpfViewer = new HPFViewer(this, "overview_id", "Overview");
			addPage((IFormPage) this.hpfViewer);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	private class ResourceChangeListener implements IResourceChangeListener {
		private ResourceChangeListener() {
		}

		public void resourceChanged(IResourceChangeEvent event) {
			IFile f = ((IFileEditorInput) HPFEditor.this.getEditorInput()).getFile();
			if (!f.exists()) {
				ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
				HPFEditor.this.close(false);
			}
		}
	}
	
	@Override
	protected void setInput(IEditorInput input) {
		super.setInput(input);
		setPartName(getEditorInput().getName());
		ResourcesPlugin.getWorkspace().addResourceChangeListener(new ResourceChangeListener(), 1);
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		// TODO Auto-generated method stub
	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}
}
